////////////////////////////////////////////////////////////////////////////////
// Copyright (C) 2008 Michiel Helvensteijn                                    //
//                                                                            //
// This file is part of the Mist compiler.                                    //
//                                                                            //
// The Mist compiler is free software: you can redistribute it and/or modify  //
// it under the terms of the GNU General Public License as published by       //
// the Free Software Foundation, either version 3 of the License, or          //
// (at your option) any later version.                                        //
//                                                                            //
// The Mist compiler is distributed in the hope that it will be useful,       //
// but WITHOUT ANY WARRANTY; without even the implied warranty of             //
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the              //
// GNU General Public License for more details.                               //
//                                                                            //
// You should have received a copy of the GNU General Public License          //
// along with the Mist compiler.  If not, see <http://www.gnu.org/licenses/>. //
////////////////////////////////////////////////////////////////////////////////

#include "ast/comparisonchain.hpp"

/******************************************************************************
 * Includes                                                                   *
 ******************************************************************************/

#include <boost/foreach.hpp>
#define foreach BOOST_FOREACH

#include <boost/shared_ptr.hpp>
using boost::dynamic_pointer_cast;

#include <climits>

#include <cstdarg>

#include "assert.hpp"

/******************************************************************************
 * Function Implementations                                                   *
 ******************************************************************************/

string to_string(ComparisonOperator comparison_operator) {
	switch (comparison_operator) {
		case EqualOperator:           return "=";
		case LessThanOperator:        return "<";
		case GreaterThanOperator:     return ">";
		case LessEqualOperator:       return "<=";
		case GreaterEqualOperator:    return ">=";
		case UnequalOperator:         return "<>";
		case ComparableOperator:      return "<>=";
		case NotEqualOperator:        return "!=";
		case NotLessOperator:         return "!<";
		case NotGreaterOperator:      return "!>";
		case NotLessEqualOperator:    return "!<=";
		case NotGreaterEqualOperator: return "!>=";
		case NotUnequalOperator:      return "!<>";
		case NotComparableOperator:   return "!<>=";
		case IdentityOperator:        return "==";
		case NotIdentityOperator:     return "!==";
		default: assert(false);
	}
}

shared_ptr<ComparisonChain> ComparisonChain::create(shared_ptr<Expression> operand1, ComparisonOperator operator1, shared_ptr<Expression> operand2) {
	return shared_ptr<ComparisonChain>(new ComparisonChain(operand1, operator1, operand2));
}

ComparisonChain::ComparisonChain(shared_ptr<Expression> operand1, ComparisonOperator operator1, shared_ptr<Expression> operand2) {
	assert(operand1);
	assert(operand2);
	
	_operands.push_back(operand1);
	_comparison_operators.push_back(operator1);
	_operands.push_back(operand2);
}

bool ComparisonChain::equals(shared_ptr<Node> other) {
	assert(other);
	
	if (typeid(*other) != typeid(ComparisonChain))
		return false;
	
	shared_ptr<ComparisonChain> cother(dynamic_pointer_cast<ComparisonChain>(other));
	
	if (comparison_operator_count() != cother->comparison_operator_count())
		return false;
	
	unsigned int i;
	for (i = 0; i < comparison_operator_count(); ++i) {
		if (!operands()[i]->equals(cother->operands()[i]))
			return false;
		
		if (comparison_operators()[i] != cother->comparison_operators()[i])
			return false;
	}
	
	return operands()[i]->equals(cother->operands()[i]);
}

const vector<Comparison> ComparisonChain::comparisons() const {
	vector<Comparison> result;
	
	for (unsigned i = 0; i+1 < operand_count(); ++i) {
		Comparison c;
		c.left_operand = operands()[i];
		c.comparison_operator = comparison_operators()[i];
		c.right_operand = operands()[i+1];
		result.push_back(c);
	}
	
	return result;
}
